/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						      color_sum.cpp

	$Header: /resource_editor/color_sum.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "color_sum.h"

#include <memory.h>
#include "color_count.h"
#include "pixel_24.h"
//------------------------------------------------------------
// color_sum.cpp
//------------------------------------------------------------

t_color_sum::t_color_sum()
{
	memset( m_sum, 0, sizeof(m_sum));
	m_square_sum = 0;
	m_count = 0;
	m_variance = 0;
}

//------------------------------------------------------------
// compute the partial variance
//------------------------------------------------------------
__int64 t_color_sum::get_delta() const
{
	int i;
	__int64 result = 0;
	__int64 value;

	for (i = 0; i < 3; i++)
	{
		value = __int64(m_sum[i]);
		result -= value * value;
	}
	return result / m_count;
}

//------------------------------------------------------------
// compute the average, or center, point for the color volume
//------------------------------------------------------------
t_pixel_24 t_color_sum::get_average() const
{
	t_pixel_24 result;

	result.red   = m_sum[k_red_axis]   / m_count;
	result.green = m_sum[k_green_axis] / m_count;
	result.blue  = m_sum[k_blue_axis]  / m_count;
	return result;
}

//------------------------------------------------------------
// add a color point to the sum
//------------------------------------------------------------
t_color_sum& t_color_sum::operator+=( t_color_count const& point )
{
	int i;

	for (i = 0; i < 3; i++)
		m_sum[i] += point.product[i];
	m_square_sum += point.square_sum;
	m_count      += point.count;
	return *this;
}

//------------------------------------------------------------
// subtract a color point from the sum
//------------------------------------------------------------
t_color_sum& t_color_sum::operator-=( t_color_count const& point )
{
	int i;

	for (i = 0; i < 3; i++)
		m_sum[i] -= point.product[i];
	m_square_sum -= point.square_sum;
	m_count      -= point.count;
	return *this;
}
